//引入模块
const http = require("http");
const url = require("url");
const ejs = require("ejs");

http.createServer(function (request, response) {
    response.writeHead(200, {"Content-Type": "text/html;charset=UTF-8"});
    let pathname = url.parse(request.url).pathname;
    let method = request.method.toLocaleLowerCase();

    if (pathname === "/login") {
        ejs.renderFile("./views/login.html", {}, function (err, data) {
            response.end(data);
        });
    } else if (pathname === "/dologin" && method === "get") {
        let getData = url.parse(request.url).query;
        response.end(decodeURI(getData));
    } else if (pathname === "/dologin" && method === "post") {
        let postStr = "";
        request.on("data", function (postData) {
            postStr += postData;
        });
        request.once("end", function () {
            postStr = decodeURI(postStr);
            response.end("<script>alert('登陆成功\\n" + postStr + "');history.back();</script>");
        });
    }
}).listen(8080);

console.log("nodejs 8080 port is starting...");